package com.dp.springsourcecodeanalysis.helper;

import com.dp.springsourcecodeanalysis.dto.ResponseDTO;
import com.dp.springsourcecodeanalysis.enums.HttpEnum;

/**
 * 返回DTO帮助类
 *
 * @author shala
 */
public class ResponseHelper {

    public static <T> ResponseDTO<T> success(T data) {
        return result(HttpEnum.OK.getCode(), data, "success");
    }

    public static <T> ResponseDTO<T> success() {
        return success(null);
    }

    public static <T> ResponseDTO<T> error(int code, String msg) {
        return result(code, null, msg);
    }

    public static <T> ResponseDTO<T> error(T data, String msg) {
        return result(HttpEnum.ERR.getCode(), data, msg);
    }

    public static <T> ResponseDTO<T> error(String msg) {
        return result(HttpEnum.ERR.getCode(), null, msg);
    }

    public static <T> ResponseDTO<T> result(int code, T data, String msg) {
        ResponseDTO<T> dto = new ResponseDTO<>();
        dto.setCode(code);
        dto.setInfo(data);
        dto.setMsg(msg);
        return dto;
    }
}
